<?php
class Admin_Model_Users extends Zend_Db_Table{
	protected $_name = 'user';
	protected $_primary = 'userid';
	
	public function countItem($arrParam = null, $option = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter, $config);
		$ssFilter = $arrParam['ssFilter'];
	
		$select = $db->select()
					 ->from('user AS u', array('COUNT(u.userid) AS totalItem'));
		if(!empty($ssFilter['keywords'])){
			$keywords = '%' . $ssFilter['keywords'] . '%';
			$select->where('u.username LIKE ?', $keywords); // doi so thu 3 ep kieu STRING
		}
		if($ssFilter['group_id']>0){
			$select->where('u.group_id = ?', $ssFilter['group_id']); // , INTEGER
		}
	
		$result = $db->fetchOne($select);
		return $result;
	}
	
	public function listItem($arrParam = null, $option = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter, $config);
		$paginator = $arrParam['paginator'];
		$ssFilter = $arrParam['ssFilter'];
	
		if($option['task'] == 'admin-list'){
			$select = $db->select()
			->from('user AS u', array('userid', 'username', 'status', 'email', 'register_date'))
			->joinLeft('user_group AS g', 'g.id = u.group_id', 'group_name');
				
			if(!empty($ssFilter['col']) && !empty($ssFilter['order'])){
				$select->order($ssFilter['col'] . ' ' . $ssFilter['order']);
			}
				
				
			if($paginator['itemCountPerPage']>0){
				$page = $paginator['currentPage'];
				$rowCount = $paginator['itemCountPerPage'];
				$select->limitPage($page, $rowCount);
			}
			if(!empty($ssFilter['keywords'])){
				$keywords = '%' . $ssFilter['keywords'] . '%';
				$select->where('u.username LIKE ?', $keywords); // doi so thu 3 ep kieu STRING
			}
			
			if($ssFilter['group_id']>0){
				$select->where('u.group_id = ?', $ssFilter['group_id']); // , INTEGER
			}
				
			$result = $db->fetchAll($select);
		}
	
		return $result;
	}
	
	public function saveItem($arrParam = null, $option = null){
		$encode = new Australian_Encode();
		if($option['task'] == 'admin-add'){
			$row = $this->fetchNew();
			$row->username 		= $arrParam['username'];
			$row->avatar 		= $arrParam['avatar'];
			$row->password 		= $encode->password($arrParam['password']);
			$row->email 		= $arrParam['email'];
			$row->group_id 		= $arrParam['group_id'];
			$row->fullname 		= $arrParam['fullname'];
			$row->birthday 		= $arrParam['birthday'];
			$row->status 		= $arrParam['status'];
			$row->sign 			= $arrParam['sign'];
			$row->register_date = date("Y-m-d");
			$row->register_ip 	= $_SERVER['REMOTE_ADDR'];
				
			$row->save();
		}
		if($option['task'] == 'admin-edit'){
			$where = 'userid = ' . $arrParam['id'];
			$row = $this->fetchRow($where);
			$row->username 		= $arrParam['username'];
			$row->avatar 		= $arrParam['avatar'];
			if(!empty($arrParam['password'])){
				$row->password 		= $encode->password($arrParam['password']);
			}
			$row->email 		= $arrParam['email'];
			$row->group_id 		= $arrParam['group_id'];
			$row->fullname 		= $arrParam['fullname'];
			$row->birthday 		= $arrParam['birthday'];
			$row->status 		= $arrParam['status'];
			$row->sign 			= $arrParam['sign'];
	
			$row->save();
		}
	}
	
	public function getItem($arrParam = null, $option = null){
		
		if($option['task'] == 'admin-info' || $option['task'] == 'admin-edit'){
			$db = Zend_Registry::get('connectDb');
			$select = $db->select()
						 ->from('user AS u')
						 ->joinLeft('user_group AS g', 'u.group_id = g.id', array('group_name'))
						 ->where('u.userid = ?', $arrParam['id']); //INTEGER
			$result = $db->fetchRow($select);
		}
		
		if($option['task'] == 'delete'){
			$where = 'userid = ' . $arrParam['id'];
			$result = $this->fetchRow($where)->toArray();
		}
	
		return $result;
	}
	
	public function deleteItem($arrParam = null, $option = null){
	
		if($option['task'] == 'admin-delete'){
			// Lay ten hinh avatar
			$row = $this->getItem($arrParam, array('task'=>'delete'));
			
			// Xoa user avatar
			$upload_dir = FILES_PATH . '/users';
			$upload = new Australian_File_Upload();
			$upload->removeFile($upload_dir . '/orignal/' . $row['avatar']);
			$upload->removeFile($upload_dir . '/img100x100/' . $row['avatar']);
			$upload->removeFile($upload_dir . '/img450x450/' . $row['avatar']);
			
			$where = 'userid = ' .$arrParam['id'];
			$this->delete($where);
		}
	
		if($option['task'] == 'admin-multi-delete'){
			$cid = $arrParam['cid'];
			if(count($cid)>0){
				if($arrParam['type'] == 1){
					$status = 1;
				}else{
					$status = 0;
				}
				foreach($cid as $key){
					$arrParam['id'] = $key;
					$row = $this->getItem($arrParam, array('task'=>'delete'));
					$upload_dir = FILES_PATH . '/users';
					$upload = new Australian_File_Upload();
					$upload->removeFile($upload_dir . '/orignal/' . $row['avatar']);
					$upload->removeFile($upload_dir . '/img100x100/' . $row['avatar']);
					$upload->removeFile($upload_dir . '/img450x450/' . $row['avatar']);
				}
				
				$ids = implode(',', $cid);
				$where = 'userid IN (' . $ids . ')';
				$this->delete($where);
			}
		}
	}
	
	public function changeStatua($arrParam = null, $option = null){
		$cid = $arrParam['cid'];
		if(count($cid)>0){
			if($arrParam['type'] == 1){
				$status = 1;
			}else{
				$status = 0;
			}
			$ids = implode(',', $cid);
			$data = array('status'=>$status);
			$where = 'userid IN (' . $ids . ')';
			$this->update($data, $where);
			// UPDATE FROM user_group SET status = 1 WHERE id IN (1,2,3)
		}
		if($arrParam['id']>0){
			if($arrParam['type'] == 1){
				$status = 1;
			}else{
				$status = 0;
			}
			
			$data = array('status'=>$status);
			$where = 'userid = ' . $arrParam['id'];
			$this->update($data, $where);
		}
	}	
}
